package com.detrum.sportcamera.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.detrum.sportcamera.R;
import com.detrum.sportcamera.adapter.CodeRateAdapter;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link CodeRateFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link CodeRateFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CodeRateFragment extends Fragment implements View.OnClickListener{
    // TODO: Rename parameter arguments, choose names that match

    // TODO: Rename and change types of parameters
    private int mParam1;
    private int flag = 4;
    private TextView exquisite, good, normal;
    private GridView codeRate_gv;
    private CodeRateAdapter codeRateAdapter;

    private OnFragmentInteractionListener mListener;

    public CodeRateFragment() {
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment CodeRateFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static CodeRateFragment newInstance() {
        CodeRateFragment fragment = new CodeRateFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_code_rate, container, false);
        codeRate_gv = view.findViewById(R.id.codeRate_gv);
        exquisite = view.findViewById(R.id.exquisite);
        good = view.findViewById(R.id.good);
        normal = view.findViewById(R.id.normal);
        exquisite.setOnClickListener(this);
        good.setOnClickListener(this);
        normal.setOnClickListener(this);
        return view;
    }

    public void setParamsSuccess(int ratiosMode){
        codeRateAdapter.setPosition(ratiosMode);
    }

    public void setmParam1(int mParam1) {
        this.mParam1 = mParam1;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onResume() {
        super.onResume();
        codeRateAdapter = new CodeRateAdapter(getActivity(),mParam1);
        codeRate_gv.setAdapter(codeRateAdapter);
        codeRate_gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mListener.selectItem(flag,position);
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onClick(View v) {
        if(v==exquisite){
            mListener.selectItem(flag,0);
        }else if(v==good){
            mListener.selectItem(flag,1);
        }else if(v==normal){
            mListener.selectItem(flag,2);
        }
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
        void selectItem(int flag, int num);
    }
}
